/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.task.common.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.task.common.entity.Task;
import com.thinkgem.jeesite.modules.task.common.service.TaskService;

/**
 * 统计考勤定时任务Controller
 * @author 郑晓乐
 * @version 2017-07-24
 */
@Controller
@RequestMapping(value = "${adminPath}/task/task")
public class TaskController extends BaseController {

	@Resource(name = "kqTask")
	private TaskService taskService;
	
	@ModelAttribute
	public Task get(@RequestParam(required=false) String id) {
		Task entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = taskService.get(id);
		}
		if (entity == null){
			entity = new Task();
		}
		return entity;
	}
	
	@RequiresPermissions("task:task:view")
	@RequestMapping(value = {"list", ""})
	public String list(Task task, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Task> page = taskService.findPage(new Page<Task>(request, response), task); 
		model.addAttribute("page", page);
		return "modules/task/taskList";
	}

	@RequiresPermissions("task:task:view")
	@RequestMapping(value = "form")
	public String form(Task task, Model model) {
		model.addAttribute("task", task);
		return "modules/task/taskForm";
	}

	@RequiresPermissions("task:task:edit")
	@RequestMapping(value = "save")
	public String save(Task task, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, task)){
			return form(task, model);
		}
		taskService.save(task);
		addMessage(redirectAttributes, "保存统计考勤定时任务成功");
		return "redirect:"+Global.getAdminPath()+"/task/task/?repage";
	}
	
	@RequiresPermissions("task:task:edit")
	@RequestMapping(value = "delete")
	public String delete(Task task, RedirectAttributes redirectAttributes) {
		taskService.delete(task);
		addMessage(redirectAttributes, "删除统计考勤定时任务成功");
		return "redirect:"+Global.getAdminPath()+"/task/task/?repage";
	}

}